<?php

namespace App\Http\Controllers\supplier;

use App\Http\Controllers\Controller;
use App\Http\Middleware\Supplier;
use App\Jobs\OrderCallbackNotify;
use App\Models\Mysql\CombinedOrders;
use App\Models\Mysql\CycleOrder;
use App\Models\Mysql\XnOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SupplierOrderController extends Controller
{
    //供应商订单
    public function order_list(Request $request){
        $params = $request->all();
        $where = [];
        $where[] = ['recharge_time','>=',$params['start_time']];
        $where[] = ['recharge_time','<=',$params['end_time']];
        $where[] = ['order_type',1];
        if (!empty($params['account'])){
            $where[] = ['account'=>$params['account']];
        }
        if (!empty($params['order_sn'])){
            $where[] = ['order_sn'=>$params['order_sn']];
        }
        if (!empty($params['recharge_status'])){
            $where[] = ['recharge_status'=>$params['recharge_status']];
        }
        if (!empty($params['merchant_number'])){
            $where[] = ['merchant_number'=>$params['merchant_number']];
        }
        if (!empty($params['goods_code'])){
            $where[] = ['goods_code'=>$params['goods_code']];
        }
        $res = Db::table('xn_order')->where($where)->orderBy('recharge_time','desc')->paginate(10);
        if (!$res) {
            return $this->sendError('查询失败');
        }
        return $this->sendResponse($res,'查询成功');
    }
    //供应商订单
    public function invoicing_order_list(Request $request){
        $params = $request->all();
        $where = [];
        $where[] = ['recharge_time','>=',$params['start_time']];
        $where[] = ['recharge_time','<=',$params['end_time']];
        $where[] = ['order_type',2];
        if (!empty($params['account'])){
            $where[] = ['account'=>$params['account']];
        }
        if (!empty($params['order_sn'])){
            $where[] = ['order_sn'=>$params['order_sn']];
        }
        if (!empty($params['recharge_status'])){
            $where[] = ['recharge_status'=>$params['recharge_status']];
        }
        if (!empty($params['merchant_number'])){
            $where[] = ['merchant_number'=>$params['merchant_number']];
        }
        if (!empty($params['goods_code'])){
            $where[] = ['goods_code'=>$params['goods_code']];
        }
        $res = Db::table('xn_order')
            ->where($where)
            ->orderBy('recharge_time','desc')
            ->paginate(10);
        if (!$res) {
            return $this->sendError('查询失败');
        }
        return $this->sendResponse($res,'查询成功');
    }
    //供应商账单
    public function bill_list(Request $request){
        $params = $request->all();
        $where[] = ['ctime','>=',$params['start_time']];
        $where[] = ['ctime','<=',$params['end_time']];
        $where[] = ['order_type',1];
        $where = [];
        if (!empty($params['order_sn'])){
            $where[] = ['order_sn'=>$params['order_sn']];
        }
        if (!empty($params['goods_id'])){
            $where[] = ['goods_id'=>$params['goods_id']];
        }
        if (!empty($params['goods_belong'])){
            $where[] = ['goods_belong'=>$params['goods_belong']];
        }
        if (!empty($params['recharge_status'])){
            $where[] = ['recharge_status'=>$params['recharge_status']];
        }
        if (!empty($params['bookkeeping_type'])){
            $where[] = ['bookkeeping_type'=>$params['bookkeeping_type']];
        }
        $res = Db::table('xn_order_bill')->where($where) ->orderBy('ctime','desc')->paginate(10);
        if (!$res) {
            return $this->sendError('查询失败');
        }
        return $this->sendResponse($res,'查询成功');
    }
    //供应商开票账单
    public function invoicing_bill_list(Request $request){
        $params = $request->all();
        $where[] = ['ctime','>=',$params['start_time']];
        $where[] = ['ctime','<=',$params['end_time']];
        $where[] = ['order_type',2];
        $where = [];
        if (!empty($params['order_sn'])){
            $where[] = ['order_sn'=>$params['order_sn']];
        }
        if (!empty($params['goods_id'])){
            $where[] = ['goods_id'=>$params['goods_id']];
        }
        if (!empty($params['goods_belong'])){
            $where[] = ['goods_belong'=>$params['goods_belong']];
        }
        if (!empty($params['recharge_status'])){
            $where[] = ['recharge_status'=>$params['recharge_status']];
        }
        if (!empty($params['bookkeeping_type'])){
            $where[] = ['bookkeeping_type'=>$params['bookkeeping_type']];
        }
        $res = Db::table('xn_order_bill')->where($where) ->orderBy('ctime','desc')->paginate(10);
        if (!$res) {
            return $this->sendError('查询失败');
        }
        return $this->sendResponse($res,'查询成功');
    }
    //供应商订单操作
    public function order_edit(Request $request)
    {
        $params = $request->all();
        if (empty($params['order_id'])){
            return $this->sendError('订单id不可为空');
        }
        $result = $params['status'] == 1;
        /** @var Supplier $supplier **/
        $supplier = $this->request->attributes->get('supplier');
        // 操作
        XnOrder::query()
            ->whereIn('id',explode(',',$params['order_id']))->get()
            ->map(fn(XnOrder $order) => OrderCallbackNotify::dispatch($supplier, $order, $result)->onQueue('merchant_notify'));

        return $this->sendResponse([],'操作成功');
    }
    //循环冲订单查询
    public function eachOrder(Request $request)
    {
        $order_sn = $request->get('order_sn');
        if (!$order_sn) {
            return $this->sendError('缺少订单号');
        }
        // 查询
        $db = CycleOrder::query()
            ->where('order_no', $order_sn)
            ->get()
            ->append(['state_status'])
            ->map(function (CycleOrder $order) {
                $order->supplier_status = $order->belong?->title ?? '未知';
                return $order;
            })
            ->toArray();
        return $this->sendResponse($db, '获取成功');
    }

    //组合商品查询
    public function fullorder(Request $request)
    {
        $order_sn = $request->get('order_sn');
        if (!$order_sn) {
            return $this->sendError('缺少订单号');
        }
        // 查询
        $db = CombinedOrders::query()
            ->where('order_no', $order_sn)
            ->get()
            ->append(['state_status'])
            ->map(function (CombinedOrders $order) {
                $order->supplier_status = $order->belong?->title ?? '未知';
                return $order;
            })
            ->toArray();
        return $this->sendResponse($db, '获取成功');
    }
}
